package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"server/internal/service"
)

type UserApi struct {
	userService *service.UserService
}

func NewUserApi(service *service.UserService) *UserApi {
	return &UserApi{
		userService: service,
	}
}

func (userApi *UserApi) UserInfo(c *gin.Context) {
	user, err := userApi.userService.UserInfo(c)

	if err != nil {
		Error(c, 111, err, "未找到指定用户")
	} else {
		OK(c, user, "success")
	}
}

func Hello(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"message": "Hello World!",
	})
}

type Response struct {
	Code int         `json:"code" example:"200"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

func (res *Response) ReturnOK() *Response {
	res.Code = 200
	return res
}

func (res *Response) ReturnError(code int) *Response {
	res.Code = code
	return res
}

func OK(c *gin.Context, data interface{}, msg string) {
	var res Response
	res.Data = data
	if msg != "" {
		res.Msg = msg
	}
	c.JSON(http.StatusOK, res.ReturnOK())
}

func Error(c *gin.Context, code int, err error, msg string) {
	var res Response
	res.Msg = err.Error()
	if msg != "" {
		res.Msg = msg
	}
	c.JSON(http.StatusOK, res.ReturnError(code))
}
